﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using GMap.NET.WindowsPresentation;
using System.Windows.Media.Animation;
using GMap.NET;
using System.Windows.Media.Effects;
using System.Net;

namespace InteractiveMapApp.CustomMarkers
{
    /// <summary>
    /// Interaction logic for CustomMarkerProjector.xaml
    /// </summary>
    public partial class CustomMarkerProjector// : UserControl
    {
        //Popup Popup;
        GMapMarker Marker;
        MainWindow MainWindow;
        Image speechBubble;

        static ImageSource altSpeechBubbleSource = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/read_bg_alt.png", UriKind.Relative));
        static ImageSource speechBubbleSource = new BitmapImage(new Uri("/InteractiveMapApp;component/Images/read_bg.png", UriKind.Relative));

        Image category = new Image();
        Image timeslot = new Image();
        Image museumImage;
        Image reportButton;
        Grid grid;
        GPoint markersLocalPosition;
        static int speechBubbleMaxSizeX = 500;
        static int speechBubbleMaxSizeY = 360;
        TextBlock msg;
        TextBox title;
        Storyboard popupSizeIncreaseStoryBoard = new Storyboard();
        Storyboard popupSizeDecreaseStoryBoard = new Storyboard();
        public bool thisMarkersPopupIsShowing = false;
        public long storyID;
        bool alternateAnimation = false;

        System.Windows.Forms.Timer storyBubbleLifeTimer;
        DoubleAnimation scale = new DoubleAnimation();
        DoubleAnimation anim = new DoubleAnimation();
        DropShadowEffect shadowEffect;
        ScaleTransform oTransform;
        public bool isItACurrentUserStory;
        string museumImageURL;

        public bool showThisBubbleWhenCreating;
        string storytext;
        string storytitle;

        /// <summary>
        /// Instantiates the markers that are pinned on the main map canvas
        /// </summary>
        /// <param name="window">reference to the main window</param>
        /// <param name="marker">reference to the actual marker</param>
        /// <param name="categoryMarkerImage">the image for the custom marker(pin)</param>
        /// <param name="categoryIconImage">the image of the category</param>
        /// <param name="timeSlotIconImage">the image of the timeslot</param>
        /// <param name="storyText">the text of the story</param>
        /// <param name="storiesID">the id of the story</param>
        /// <param name="showBubbleOnCreation">true to display the bubble when it is added to the map</param>
        /// <param name="isItUserStory">true if the story is to be considered special as a user story</param>
        public CustomMarkerProjector(MainWindow window, GMapMarker marker, ImageSource categoryMarkerImage, ImageSource categoryIconImage, ImageSource timeSlotIconImage, string museumImageUrl, string storyText, string storyTitle, long storiesID, bool showBubbleOnCreation, bool isItUserStory)
        {
            this.storyID = storiesID;
            this.InitializeComponent();
            this.MainWindow = window;
            this.Marker = marker;
            //this.Popup = new Popup();
            this.AllowDrop = false;
            this.grid = new Grid();
            this.msg = new TextBlock();
            this.title = new TextBox();
            this.speechBubble = new Image();
            this.isItACurrentUserStory = isItUserStory;
            this.showThisBubbleWhenCreating = showBubbleOnCreation;

            this.icon.Source = categoryMarkerImage;
            this.category.Source = categoryIconImage;
            this.timeslot.Source = timeSlotIconImage;
            this.museumImageURL = museumImageUrl;
            this.storytext = storyText.Replace("`", "'");//replaces the tilde (`) with a single quote (')
            this.storytitle = storyTitle.Replace("`", "'");//replaces the tilde (`) with a single quote (')

            this.InitialiseThis();
        }

        private void InitialiseThis()
        {
            this.Unloaded += new RoutedEventHandler(CustomMarker_Unloaded);
            this.Loaded += new RoutedEventHandler(CustomMarker_Loaded);
            this.SizeChanged += new SizeChangedEventHandler(CustomMarker_SizeChanged);
            //MainWindow.MainMap.MouseDown += new MouseButtonEventHandler(MainMap_MouseDown);
            //MainWindow.MainMap.OnMapZoomChanged += new GMap.NET.MapZoomChanged(MainMap_OnMapZoomChanged);
            this.popupSizeDecreaseStoryBoard.Completed += new EventHandler(popupSizeDecreaseStoryBoard_Completed);
            this.popupSizeIncreaseStoryBoard.Completed += new EventHandler(popupSizeIncreaseStoryBoard_Completed);
            //this.MouseEnter += new MouseEventHandler(MarkerControl_MouseEnter);
            //this.MouseLeave += new MouseEventHandler(MarkerControl_MouseLeave);
            //this.TouchUp += new EventHandler<TouchEventArgs>(CustomMarker_TouchUp);
            //this.TouchDown += new EventHandler<TouchEventArgs>(CustomMarker_TouchDown);

            //Popup.AllowsTransparency = true;            
            //Popup.PlacementTarget = this;
            //Popup.Name = "Popup";
            grid.Name = "Popup";
            this.Name = "Marker";
            //Popup.Placement = PlacementMode.Top;
            ////Popup.HorizontalOffset = -1 * (this.icon.Width);
            ////Popup.VerticalOffset = -1 * (this.icon.Height);
            //Popup.HorizontalOffset = 0 ;//- ((this.Width / 2) - 5);
            //Popup.VerticalOffset = 0;//+this.Height;
            //Popup.Height = 0;
            //Popup.Width = 0;

            NameScope.SetNameScope(this, new NameScope());
            NameScope.GetNameScope(this).RegisterName(this.Name, RenderTransform);
            NameScope.GetNameScope(this).RegisterName(grid.Name, grid);

            string StoryText = "";
            ///cuts the string short to fit in bubble if it is too big
            if (storytext.Length > 325)
            {
                //Console.WriteLine(title);
                StoryText = storytext.Substring(0, 325) + " ..";
            }
            else
            {
                StoryText = storytext;
            }
            
            string TitleText = "";
            ///cuts the string short to fit in bubble if it is too big
            if (storytitle.Length > 45)
            {
                //Console.WriteLine(title);
                TitleText = storytitle.Substring(0, 45) + " ..";
            }
            else
            {
                TitleText = storytitle;
            }

            msg.Padding = new Thickness(24, 100, 50, 110);
            msg.MaxWidth = speechBubbleMaxSizeX;
            msg.MaxHeight = speechBubbleMaxSizeY - 6;

            msg.TextWrapping = TextWrapping.Wrap;
            msg.Text = StoryText;
            msg.FontFamily = new FontFamily("Lucida Sans Unicode");
            msg.IsHitTestVisible = false;

            msg.FontSize = 20;
            msg.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            msg.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

            title.Margin = new Thickness(20, 70, 0, 0);
            title.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            title.MaxWidth = speechBubbleMaxSizeX;
            title.MaxHeight = 40;
            title.MaxLines = 1;
            title.BorderThickness = new Thickness(0,0,0,0);
            title.TextWrapping = TextWrapping.NoWrap;
            title.Text = TitleText.ToUpper();
            title.Background = Brushes.Transparent;
            title.FontFamily = new FontFamily("Lucida Sans Unicode");
            title.IsHitTestVisible = false;
            title.FontSize = 20;
            title.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            title.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

            speechBubble.Source = speechBubbleSource;//new BitmapImage(new Uri("/InteractiveMapApp;component/Images/read_bg.png", UriKind.Relative));

            speechBubble.Stretch = Stretch.Fill;//Stretch.Uniform;
            //speechBubble.Effect = new System.Windows.Media.Effects.DropShadowEffect();
            category.Height = 45;
            //category.Width = 100;
            timeslot.Height = 60;
            //timeslot.Width = 100;

            timeslot.Margin = new Thickness(35, 12, 0, 0);
            category.Margin = new Thickness(150, 20, 0, 0);
            //category.Margin = new Thickness(16, 0, 380, 75);

            timeslot.VerticalAlignment = VerticalAlignment.Top;
            category.VerticalAlignment = VerticalAlignment.Top;

            timeslot.HorizontalAlignment = HorizontalAlignment.Left;
            category.HorizontalAlignment = HorizontalAlignment.Left;

            //timeslot.Stretch = Stretch.UniformToFill;
            //category.Stretch = Stretch.UniformToFill;

            grid.Children.Add(speechBubble);
            grid.Children.Add(category);
            grid.Children.Add(timeslot);



            storyBubbleLifeTimer = new System.Windows.Forms.Timer();

            //Popup.Child = grid;
            //Popup.Opacity = 0.8;

            setupPopupSizeDecreaseStoryBoard();
            setupPopupSizeIncreaseStoryboard();

            if (showThisBubbleWhenCreating)
            {
                storyBubbleLifeTimer.Interval = 45000; //20 seconds
                storyBubbleLifeTimer.Tick += new EventHandler(storyBubbleLifeTimer_Tick);
                storyBubbleLifeTimer.Start();

                showThisBubbleWhenCreating = false;
                this.showMarker();
            }
        }

        
        private void setupPopupSizeIncreaseStoryboard()
        {
            //DoubleAnimation scaleMarkerX = new DoubleAnimation();
            //scaleMarkerX.From = 1;
            //scaleMarkerX.To = 2;
            //scaleMarkerX.BeginTime = TimeSpan.FromSeconds(0.30);
            //scaleMarkerX.Duration = new Duration(TimeSpan.FromSeconds(0.15));
            //Storyboard.SetTargetProperty(scaleMarkerX, new PropertyPath("RenderTransform.(ScaleTransform.ScaleX)"));

            //DoubleAnimation scaleMarkerY = new DoubleAnimation();
            //scaleMarkerY.From = 1;
            //scaleMarkerY.To = 2;
            //scaleMarkerY.Duration = new Duration(TimeSpan.FromSeconds(0.15));
            //Storyboard.SetTargetProperty(scaleMarkerY, new PropertyPath("RenderTransform.(ScaleTransform.ScaleY)"));
            //ScaleTransform scaleTrans = new ScaleTransform();
            //scaleTrans.CenterX = 0;
            //scaleTrans.CenterY = 0;

            //this.RenderTransform = scaleTrans;

            //DoubleAnimation visibleMarker = new DoubleAnimation();
            //visibleMarker.From = 1;
            //visibleMarker.To = 0;
            //scaleMarkerX.BeginTime = TimeSpan.FromSeconds(0.5);
            //visibleMarker.Duration = new Duration(TimeSpan.FromSeconds(0.5));
            //Storyboard.SetTarget(visibleMarker, this.icon);
            //Storyboard.SetTargetProperty(visibleMarker, new PropertyPath(UIElement.OpacityProperty));

            DoubleAnimation sizeAniX = new DoubleAnimation();
            sizeAniX.From = 0;
            sizeAniX.To = speechBubbleMaxSizeX;
            sizeAniX.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            Storyboard.SetTargetName(sizeAniX, grid.Name);
            Storyboard.SetTargetProperty(sizeAniX, new PropertyPath(Popup.WidthProperty));

            DoubleAnimation sizeAniY = new DoubleAnimation();
            sizeAniY.From = 0;
            sizeAniY.To = speechBubbleMaxSizeY;
            sizeAniY.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            Storyboard.SetTargetName(sizeAniY, grid.Name);
            Storyboard.SetTargetProperty(sizeAniY, new PropertyPath(Popup.HeightProperty));

            if (!alternateAnimation)
            {
                DoubleAnimation translateAnim = new DoubleAnimation();
                translateAnim.Duration = TimeSpan.FromSeconds(0.25);
                translateAnim.From = +(this.Width / 2) + 5;
                translateAnim.To = -((this.Width / 2) - 5);
                Storyboard.SetTargetName(translateAnim, grid.Name);
                Storyboard.SetTargetProperty(translateAnim, new PropertyPath(Popup.HorizontalOffsetProperty));
                this.popupSizeIncreaseStoryBoard.Children.Add(translateAnim);
            }
            //this.popupSizeIncreaseStoryBoard.Children.Add(scaleMarkerX);
            //this.popupSizeIncreaseStoryBoard.Children.Add(scaleMarkerY);
            //this.popupSizeIncreaseStoryBoard.Children.Add(visibleMarker);

            this.popupSizeIncreaseStoryBoard.Children.Add(sizeAniX);
            this.popupSizeIncreaseStoryBoard.Children.Add(sizeAniY);
            
        }

        void popupSizeIncreaseStoryBoard_Completed(object sender, EventArgs e)
        {
            if (!grid.Children.Contains(msg))
            {
                msg.Text += " ";
                grid.Children.Add(msg);
            }

            if (!grid.Children.Contains(title))
            {
                title.Text += " ";
                grid.Children.Add(title);
            }

            thisMarkersPopupIsShowing = true;
        }

        private void setupPopupSizeDecreaseStoryBoard()
        {
            DoubleAnimation sizeAniX = new DoubleAnimation();
            sizeAniX.From = speechBubbleMaxSizeX;
            sizeAniX.To = 0;
            sizeAniX.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            Storyboard.SetTargetName(sizeAniX, grid.Name);
            Storyboard.SetTargetProperty(sizeAniX, new PropertyPath(Popup.WidthProperty));

            DoubleAnimation sizeAniY = new DoubleAnimation();
            sizeAniY.From = speechBubbleMaxSizeY;
            sizeAniY.To = 0;
            sizeAniY.Duration = new Duration(TimeSpan.FromSeconds(0.25));
            Storyboard.SetTargetName(sizeAniY, grid.Name);
            Storyboard.SetTargetProperty(sizeAniY, new PropertyPath(Popup.HeightProperty));

            if (!alternateAnimation)
            {
                DoubleAnimation translateAnim = new DoubleAnimation();
                translateAnim.Duration = TimeSpan.FromSeconds(0.25);
                translateAnim.To = +((this.Width / 2) - 5);
                Storyboard.SetTargetName(translateAnim, grid.Name);
                Storyboard.SetTargetProperty(translateAnim, new PropertyPath(Popup.HorizontalOffsetProperty));
                this.popupSizeIncreaseStoryBoard.Children.Add(translateAnim);
            }

            this.popupSizeDecreaseStoryBoard.Children.Add(sizeAniX);
            this.popupSizeDecreaseStoryBoard.Children.Add(sizeAniY);
           
        }

        void CustomMarker_Loaded(object sender, RoutedEventArgs e)
        {
            if (icon.Source.CanFreeze)
            {
                this.icon.Source.Freeze();
            }

            //If its a user story, apply different set of effects to 'other' stories
            if (this.isItACurrentUserStory)
            {
                this.shadowEffect = new DropShadowEffect();
                this.shadowEffect.ShadowDepth = 0;
                this.shadowEffect.BlurRadius = 20;
                this.shadowEffect.Color = Colors.White;
                this.icon.Effect = this.shadowEffect;
            }
            else
            {
                this.anim.Duration = new TimeSpan(0, 0, 0, 0, 500);
                this.scale.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100));

                this.anim.From = 0;
                this.anim.To = 1;

                this.scale.From = 0;
                this.scale.To = 1;

                this.oTransform = new ScaleTransform();
                this.oTransform.CenterX = this.Width / 2;
                this.oTransform.CenterY = this.Height;
                this.RenderTransform = oTransform;

                this.oTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scale);
                this.oTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scale);

                this.BeginAnimation(OpacityProperty, anim);
            }
        }

        void CustomMarker_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            this.Marker.Offset = new Point(-e.NewSize.Width / 2, -e.NewSize.Height);
        }

        void defer_Tick(object sender, EventArgs e)
        {
            if (sender != null)
            {
                (sender as System.Windows.Forms.Timer).Stop();
                (sender as System.Windows.Forms.Timer).Tick -= new EventHandler(defer_Tick);
                sender = null;
            }


            if (!this.thisMarkersPopupIsShowing)
            {
                this.thisMarkersPopupIsShowing = true;
                this.Marker.ZIndex += 10000;
                //Decide if MainMaps position must be shuffled down a bit to cater for being able to show this markers messagebox
                this.markersLocalPosition = MainWindow.MainMap.FromLatLngToLocal(Marker.Position);

                ///then choose a different marker
                if ((markersLocalPosition.Y < 40) || (markersLocalPosition.Y > MainWindow.Height) || (markersLocalPosition.X > MainWindow.Width) || (markersLocalPosition.X < 0))
                {
                    MainWindow.showStoryTextLoop();
                }

                else //if ((markersLocalPosition.Y < speechBubbleMaxSizeY + 20) || (markersLocalPosition.X > MainWindow.Width - speechBubbleMaxSizeX + 80) || (markersLocalPosition.X < 50))
                {
                    alternateAnimation = true;

                    speechBubble.Source = altSpeechBubbleSource;

                    // this.icon.Visibility = System.Windows.Visibility.Hidden;
                    // this.popupSizeDecreaseStoryBoard.Stop();

                    //this.Popup.IsOpen = false;
                    //this.Popup.IsOpen = true;
                    MainWindow.userIsCurrentlyReadingAStory = true;
                    //Popup.PlacementTarget = null;
                    //Popup.Placement = PlacementMode.AbsolutePoint;
                   // Popup.HorizontalOffset = 30;
                    grid.Visibility = Visibility.Visible;
                    MainWindow.grid.Children.Add(grid);
                    grid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    grid.HorizontalAlignment = HorizontalAlignment.Left;

                    if ((markersLocalPosition.Y < speechBubbleMaxSizeY + 80) && (markersLocalPosition.X < speechBubbleMaxSizeX + 50))
                    {
                        //Popup.VerticalOffset = 400;
                        grid.Margin = new Thickness(50,400,0,0);
                    }
                    else
                    {
                        //Popup.VerticalOffset = 60;
                        grid.Margin = new Thickness(50, 60, 0, 0);
                    }
                    

                    this.popupSizeIncreaseStoryBoard.Begin(this);

                    this.shadowEffect = new DropShadowEffect();
                    this.shadowEffect.ShadowDepth = 0;
                    this.shadowEffect.BlurRadius = 20;
                    this.shadowEffect.Color = Colors.Yellow;
                    this.icon.Effect = this.shadowEffect;

                    this.scale.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100));
                    this.scale.From = 1;
                    this.scale.To = 2.4;
                    this.oTransform = new ScaleTransform();
                    this.oTransform.CenterX = this.Width / 2;
                    this.oTransform.CenterY = this.Height;
                    this.RenderTransform = oTransform;
                    this.oTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scale);
                    this.oTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scale);
                }

                //else
                //{
                //    this.icon.Visibility = System.Windows.Visibility.Hidden;
                //    this.popupSizeIncreaseStoryBoard.Begin(this);
                //    this.Popup.IsOpen = true;
                //    MainWindow.userIsCurrentlyReadingAStory = true;
                //}


            }


        }

        void showMarker()
        {
            //hide any showing markers
            foreach (GMapMarker mrkr in MainWindow.MainMap.Markers)
            {
                if ((mrkr.Shape as CustomMarkerProjector).thisMarkersPopupIsShowing && !(mrkr.Shape as CustomMarkerProjector).showThisBubbleWhenCreating)
                {
                    (mrkr.Shape as CustomMarkerProjector).hideMarker();
                }
            }

            //defer these for a short time
            System.Windows.Forms.Timer defer = new System.Windows.Forms.Timer();
            defer.Interval = 2;
            defer.Tick += new EventHandler(defer_Tick);
            defer.Start();
        }

        //hides the text bubble
        void storyBubbleLifeTimer_Tick(object sender, EventArgs e)
        {
            if (storyBubbleLifeTimer != null && storyBubbleLifeTimer.Enabled)
            {
                showThisBubbleWhenCreating = false;
                storyBubbleLifeTimer.Stop();
                storyBubbleLifeTimer.Tick -= new EventHandler(storyBubbleLifeTimer_Tick);
                hideMarker();
            }
        }

        /// <summary>
        /// shows the text bubble for projector app
        /// </summary>
        public void ShowMarkerBubble()
        {
            if (!this.thisMarkersPopupIsShowing)
            {
                showMarker();
                if (storyBubbleLifeTimer != null)
                {
                    storyBubbleLifeTimer.Interval = 45000; //20 seconds
                    storyBubbleLifeTimer.Tick += new EventHandler(storyBubbleLifeTimer_Tick);
                    storyBubbleLifeTimer.Start();
                }
            }
        }

        public void hideMarker()
        {
            if (this.Marker != null && this.thisMarkersPopupIsShowing)
            {
                MainWindow.userIsCurrentlyReadingAStory = false; 
                this.thisMarkersPopupIsShowing = false;

                MainWindow.showStoryBubbleTimer.Stop();
                MainWindow.showStoryBubbleTimer.Start();


                this.Marker.ZIndex -= 10000;
                this.popupSizeIncreaseStoryBoard.Stop();
                if (grid.Children.Contains(msg))
                {
                    grid.Children.Remove(msg);
                }
                if (grid.Children.Contains(title))
                {
                    grid.Children.Remove(title);
                }
                this.popupSizeDecreaseStoryBoard.Begin(this);
            }
        }

        void popupSizeDecreaseStoryBoard_Completed(object sender, EventArgs e)
        {
            this.icon.Visibility = System.Windows.Visibility.Visible;
            //this.Popup.IsOpen = false;
            MainWindow.grid.Children.Remove(grid);
            
            if (alternateAnimation)
            {
                this.icon.Effect = null;
                this.scale.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 100));
                this.scale.From = 2.4;
                this.scale.To = 1;
                this.oTransform = new ScaleTransform();
                this.oTransform.CenterX = this.Width / 2;
                this.oTransform.CenterY = this.Height;
                this.RenderTransform = oTransform;
                this.oTransform.BeginAnimation(ScaleTransform.ScaleXProperty, scale);
                this.oTransform.BeginAnimation(ScaleTransform.ScaleYProperty, scale);

                alternateAnimation = false;
            }
        }


        /// <summary>
        /// Its massive but necessary
        /// 
        /// We identified a major memory leak without these 
        /// using ANTS .net Memory Profiler
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CustomMarker_Unloaded(object sender, RoutedEventArgs e)
        {
            //MainWindow.showStoryBubbleTimer.Stop();
            //MainWindow.showStoryBubbleTimer.Start();
            this.hideMarker();
            this.Unloaded -= new RoutedEventHandler(CustomMarker_Unloaded);
            this.Loaded -= new RoutedEventHandler(CustomMarker_Loaded);
            this.SizeChanged -= new SizeChangedEventHandler(CustomMarker_SizeChanged);
           // this.TouchDown -= new EventHandler<TouchEventArgs>(CustomMarker_TouchDown);
            //MainWindow.MainMap.MouseDown -= new MouseButtonEventHandler(MainMap_MouseDown);
            //MainWindow.MainMap.OnMapZoomChanged -= new GMap.NET.MapZoomChanged(MainMap_OnMapZoomChanged);
            this.popupSizeDecreaseStoryBoard.Completed -= new EventHandler(popupSizeDecreaseStoryBoard_Completed);
            this.popupSizeIncreaseStoryBoard.Completed -= new EventHandler(popupSizeIncreaseStoryBoard_Completed);
            //this.MouseEnter -= new MouseEventHandler(MarkerControl_MouseEnter);
            //this.MouseLeave -= new MouseEventHandler(MarkerControl_MouseLeave);
            if (storyBubbleLifeTimer != null)
            {
                storyBubbleLifeTimer.Tick -= new EventHandler(storyBubbleLifeTimer_Tick);
            }
            storyBubbleLifeTimer = null;
            this.Marker.Clear();
            this.MainWindow = null;
            this.Marker.Shape = null;
            this.Marker = null;
            this.icon.Effect = null;
            this.icon.Source = null;
            this.icon = null;
            //this.Popup = null;
            this.msg = null;
            this.title = null;
            this.speechBubble.Source = null;
            this.speechBubble = null;
            this.grid = null;
            this.category.Source = null;
            this.category = null;
            this.timeslot.Source = null;
            this.timeslot = null;
            this.popupSizeDecreaseStoryBoard = null;
            this.popupSizeIncreaseStoryBoard = null;
            this.scale = null;
            this.anim = null;
            if (this.museumImage != null)
            {
                this.museumImage.Source = null;
                this.museumImage = null;
            }

            if (this.reportButton != null)
            {
               // this.reportButton.MouseDown -= new MouseButtonEventHandler(reportButton_MouseDown);
                //this.reportButton.MouseUp -= new MouseButtonEventHandler(reportButton_MouseUp);
                this.reportButton.Source = null;
                this.reportButton = null;
            }
            if (this.shadowEffect != null)
            {
                this.shadowEffect = null;
            }
            if (this.oTransform != null)
            {
                this.oTransform = null;
            }
        }

        //void MainMap_OnMapZoomChanged()
        //{
        //    if (this.thisMarkersPopupIsShowing)
        //    {
        //        hideMarker();
        //    }
        //}
        //void MarkerControl_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    //this.Marker.ZIndex -= 10000;
        //}

        //void MarkerControl_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    this.Marker.ZIndex += 10000;
        //}

    }
}